﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam4.Biz;
using MockPrjTeam4.Entity;
using MockPrjTeam4.Common;


namespace MockPrjTeam4
{
    public partial class ProgrammeEdit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string popup = @"<script>
            function popup(url, width, height)
            {
                //var width = 500;
                //var height = 400;
                var left = (screen.width - width) / 2;
                var top = (screen.height - height) / 2;
                var params = 'width=' + width + ', height=' + height;
                params += ', top=' + top + ', left=' + left;
                params += ', directories=no';
                params += ', location=no';
                params += ', menubar=no';
                params += ', resizable=no';
                params += ', scrollbars=yes';
                params += ', status=no';
                params += ', toolbar=no';
                newwin = window.open(url, 'windowname5', params);
                if (window.focus) 
                { 
                    newwin.focus() 
                }
                return false;
            }
            </script>";
            if (!Page.ClientScript.IsStartupScriptRegistered("Mast"))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),
                    "Mast", popup);
            }
            int id = Methods.ConvertNumber(Request.QueryString["ID"]);
            Programmes pro = ProgrammeBiz.GetProgrammeById(id);
            Contacts contact = ContactBiz.GetContactById(pro.ContactID);   
            if (!IsPostBack)
            {
                txtProName.Text = pro.ProgrammeName;
                txtDes.Text = pro.Description;
                if (contact != null)
                    txtContactName.Text = contact.FirstName + " " + contact.SurName;
                else
                    txtContactName.Text = "";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int id = Methods.ConvertNumber(Request.QueryString["ID"]);
            Programmes pro = ProgrammeBiz.GetProgrammeById(id);
            pro.ProgrammeId = id;
            pro.ProgrammeName = txtProName.Text;
            pro.Description = txtDes.Text;
            if (hdfInactive.Value == "")
                pro.IsActive = true;
            else
                pro.IsActive = Methods.ConvertBoolean(hdfInactive.Value);
            string a = txtContactName.Text;
            if (hdfContact.Value == "")
            {
                Contacts ct = ContactBiz.GetContactById(pro.ContactID);
                if (ct == null)
                    pro.ContactID = -1;
                else
                    pro.ContactID = ct.ContactID;
            }
            else
                pro.ContactID = Methods.ConvertNumber(hdfContact.Value);
            ProgrammeBiz.EditProgramme(pro);
            lblMessage.Text = "You editted " + txtProName.Text + " successfully";
        }

        protected void btnDeactive_Click(object sender, EventArgs e)
        {
            hdfInactive.Value = "false";
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Views/ListProgrammes.aspx");
        }
    }
}